package com.info.zhiduoduo.core.service.impl;

import com.info.zhiduoduo.common.constants.project.ProjectType;
import com.info.zhiduoduo.core.service.CoreInvoiceAmountService;
import com.info.zhiduoduo.repository.mapper.crowdsource.CrowdsourceInvoiceMapper;
import com.info.zhiduoduo.repository.mapper.invoice.InvoiceMapper;
import com.info.zhiduoduo.repository.mapper.taxer.TaxerInvoiceMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Slf4j
@Service
public class CoreInvoiceAmountServiceImpl implements CoreInvoiceAmountService {

    @Resource
    private InvoiceMapper invoiceMapper;
    @Resource
    private CrowdsourceInvoiceMapper crowdsourceInvoiceMapper;
    @Resource
    private TaxerInvoiceMapper taxerInvoiceMapper;

    //timeType : 时间类型：1：当前月度，2：当前季度，3：当前年度，4：当前时间往前的12个月
    static int MONTH = 1;
    static int QUARTER = 2;
    static int YEAR = 3;
    static int TWELVEMONTH = 4;

    @Override
    public BigDecimal getUserMonthlyTotalAmount(int userid) {
        return getUserTotalInvoiceAmount(userid, MONTH);
    }

    @Override
    public BigDecimal getUserQuarterTotalAmount(int userid) {
        return getUserTotalInvoiceAmount(userid, QUARTER);
    }

    @Override
    public BigDecimal getUserYearlyTotalAmount(int userid) {
        return getUserTotalInvoiceAmount(userid, YEAR);
    }

    @Override
    public BigDecimal getUserTwelveMonthTotalAmount(int userid) {
        return getUserTotalInvoiceAmount(userid, TWELVEMONTH);
    }

    public BigDecimal getUserTotalInvoiceAmount(int userid, int timeType) {
        //  发票统计
        String zddProjectTypes = ProjectType.invoiceZddProjectTypes();
        BigDecimal zddInvoiceAmount = invoiceMapper.getUserCompleteAmount(userid, zddProjectTypes, timeType);
        // 众包成本票统计
        String croProjectTypes = ProjectType.invoiceCroProjectTypes();
        BigDecimal crowdsourceInvoiceAmount = crowdsourceInvoiceMapper.getUserCompleteAmount(userid, croProjectTypes, timeType);
        // 纯代理发票统计
        BigDecimal taxerInvoiceAmount = taxerInvoiceMapper.getUserCompleteAmount(userid, timeType);
        return zddInvoiceAmount.add(crowdsourceInvoiceAmount).add(taxerInvoiceAmount);
    }

    @Override
    public BigDecimal getUserMonthlyEntrustmentAmount(int userid) {
        return getUserEntrustmentInvoiceAmount(userid, MONTH);
    }

    @Override
    public BigDecimal getUserQuarterEntrustmentAmount(int userid) {
        return getUserEntrustmentInvoiceAmount(userid, QUARTER);
    }

    @Override
    public BigDecimal getUserYearlyEntrustmentAmount(int userid) {
        return getUserEntrustmentInvoiceAmount(userid, YEAR);
    }

    @Override
    public BigDecimal getUserTwelveMonthEntrustmentAmount(int userid) {
        return getUserEntrustmentInvoiceAmount(userid, TWELVEMONTH);
    }

    public BigDecimal getUserEntrustmentInvoiceAmount(int userid,int timeType) {
        // 委托 发票统计
        String zddProjectTypes = StringUtils.joinWith(",", ProjectType.ENTRUST_ZHIDUODUO, ProjectType.MAKER_GROUPON_ZHIDUODUO);
        BigDecimal zddInvoiceAmount = invoiceMapper.getUserCompleteAmount(userid, zddProjectTypes, timeType);
        // 委托众包成本票统计
        String croProjectTypes = StringUtils.joinWith(",", ProjectType.ENTRUST_CROWDSOURCE, ProjectType.MAKER_GROUPON_CROWDSOURCE);
        BigDecimal crowdsourceInvoiceAmount = crowdsourceInvoiceMapper.getUserCompleteAmount(userid, croProjectTypes, timeType);
        return zddInvoiceAmount.add(crowdsourceInvoiceAmount);
    }


    @Override
    public BigDecimal getUserInvoicingAmount(int userid) {
        return invoiceMapper.getUserInvoicingAmount(userid);
    }

    @Override
    public BigDecimal getUserQuarterStrategyInvoicingAmount(int userid) {
        return invoiceMapper.getUserQuarterStrategyInvoicingAmount(userid);
    }
}
